
import json

# .attr
TAG_ATTR_RO	= 0x1
TAG_ATTR_WO = 0x2
TAG_ATTR_RW = 0x3

# .type
TAG_TYPE_SETTING = 0
TAG_TYPE_DATA = 1

FROFILE_SUFFIX = ".cfg"


def room(name):
	node = {}

	node['name'] = name
	node['tags'] = ['temp', 'humi']
	for tag_name in node['tags']:
		node[tag_name + '.attr'] = TAG_ATTR_RO
		node[tag_name + '.type'] = TAG_TYPE_DATA

	fd = open(name + FROFILE_SUFFIX, "w")
	fd.write(json.dumps(node))
	fd.close()

	return node


def fishtank(name):
	node = {}

	node['name'] = name
	node['tags'] = ['temp', 'humi', 'heater']

	node['temp.attr'] = TAG_ATTR_RO
	node['temp.type'] = TAG_TYPE_DATA

	node['humi.attr'] = TAG_ATTR_RO
	node['humi.type'] = TAG_TYPE_DATA

	node['heater.attr'] = TAG_ATTR_WO
	node['heater.type'] = TAG_TYPE_DATA

	fd = open(name + FROFILE_SUFFIX, "w")
	fd.write(json.dumps(node))
	fd.close()

	return node


def main(gw_name):
	gw = {}
	gw['name'] = gw_name

	gw['node'] = []
	gw['node'].append(room("bedroom"))
	gw['node'].append(fishtank("fishtank"))

	# TODO: dump to file
	fd = open(gw_name + FROFILE_SUFFIX, "w")
	fd.write(json.dumps(gw))
	fd.close()


if __name__ == '__main__':
	main("gateway")
